package c2.group.business.tb.ko;

import c2.advanced.business.base.KvData;
import cate.game.pvp.throne.data.ko.TBKoBet;
import cate.game.pvp.throne.data.ko.TBKoChampionRecord;
import cate.game.pvp.throne.data.ko.TBKoMatch;
import cate.game.pvp.throne.data.ko.TBKoTimeTable;
import cate.game.pvp.throne.dto.TBKoBaseDTO;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@NoteClass("王座争霸-淘汰赛基础数据")
@Slf4j
public class TBKoStatus extends KvData {

    @NoteField("淘汰赛时间表")
    public TBKoTimeTable timeTable;

    @NoteField("竞猜数据")
    public TBKoBet bet;

    @NoteField("冠军记录")
    public Map<Integer, TBKoChampionRecord> champions;

    @Override
    public void init(String theKey) {
        super.init(theKey);
        if (timeTable == null) {
            timeTable = new TBKoTimeTable();
        }
        timeTable.initialize();
        if (bet == null) {
            bet = new TBKoBet();
        }
        bet.initialize();
        if (champions == null) {
            champions = new HashMap<>();
        }
        champions.values().forEach(TBKoChampionRecord::initialize);
    }

    public TBKoBaseDTO toDTO() {
        TBKoBaseDTO dto = new TBKoBaseDTO();
        dto.timeTable = timeTable;
        dto.bet = bet;
        dto.champions = champions;
        return dto;
    }


    public void clear() {
        timeTable.clear();
        bet.clear();
    }


    public void setChampion(int season, int levelGroup, TBKoMatch match) {
        if (match == null || match.isEmpty()) {
            return;
        }
        TBKoChampionRecord record = champions.get(season);
        if (record == null) {
            record = new TBKoChampionRecord();
            record.season = season;
            record.initialize();
            champions.put(season, record);
        }
        record.setChampion(levelGroup, match);
        log.info("王座争霸第[{}]赛季第[{}]组胜败对绝冠军是[{}]", season, levelGroup, match.getWinnerInfo().name);
    }
}
